<template>
  <div class="add-container">
    <div class="add-top">
      <el-page-header @back="goBack" :content="gobacktitle"></el-page-header>
    </div>
    <div class="add-main">
      <div class="add-main-top">
        <el-descriptions
          title="出库单"
          direction="vertical"
          :column="6"
          border
          id="descriptions"
        >
          <el-descriptions-item label="出库单号">
            {{ ruleForm.chukudh }}
          </el-descriptions-item>
          <el-descriptions-item label="名称">
            {{ ruleForm.mingcheng }}
          </el-descriptions-item>

          <el-descriptions-item label="出库状态">
            {{ $util.getCkzt(ruleForm.zhuangtai) }}
          </el-descriptions-item>
          <el-descriptions-item label="出库类型">
            {{ $util.getCklx(ruleForm.chukulx) }}
          </el-descriptions-item>
          <el-descriptions-item label="创建人">
            {{ ruleForm.createName }}
          </el-descriptions-item>
          <el-descriptions-item label="创建时间">
            {{ ruleForm.createTime }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            {{ ruleForm.tips }}
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <div class="add-main-list2">
        <div class="add-main-tital">
          <p>出库单明细</p>
        </div>
        <div class="table-list">
          <ux-grid
            border
            show-overflow
            ref="plxTable"
            keep-source
            height="tabheight"
            :highlightCurrentRow="false"
            widthResize
            :rules="ruleslist"
            :edit-config="{ trigger: 'click', mode: 'row' }"
          >
            <ux-table-column
              type="index"
              title="序号"
              width="50"
            ></ux-table-column>
            <ux-table-column
              field="mingcheng"
              title="挂架名称"
            ></ux-table-column>
            <ux-table-column field="xingbie" title="型别"></ux-table-column>
            <ux-table-column
              field="chuchanghm"
              title="出厂号码"
            ></ux-table-column>
            <ux-table-column field="weiyibm" title="唯一编码"></ux-table-column>
            <ux-table-column
              field="shengchancj"
              title="生产厂家"
            ></ux-table-column>
            <ux-table-column field="guajiazt" title="挂架状态">
              <template v-slot="scope">
                {{ $util.getguajiazt(scope.row.guajiazt) }}
              </template>
            </ux-table-column>
            <ux-table-column field="caozuozt" title="挂架操作状态">
              <template v-slot="scope">
                {{ $util.getguajiaczzt(scope.row.caozuozt) }}
              </template>
            </ux-table-column>
            <ux-table-column
              field="tips"
              title="备注"
              :edit-render="{ autofocus: '.el-input__inner' }"
            >
              <template v-slot:edit="scope">
                <el-input v-model.trim="scope.row.tips" placeholder="请输入" />
              </template>
            </ux-table-column>
            <!-- <ux-table-column
              field="qishid"
              title="起始点"
              :edit-render="{ autofocus: '.el-input__inner' }"
            >
              <template v-slot:edit="scope">
                <el-input
                  v-model.trim="scope.row.qishid"
                  placeholder="请输入"
                />
              </template>
            </ux-table-column>
            <ux-table-column
              field="zhongjied"
              title="终结点"
              :edit-render="{ autofocus: '.el-input__inner' }"
            >
              <template v-slot:edit="scope">
                <el-input
                  v-model.trim="scope.row.zhongjied"
                  placeholder="请输入"
                />
              </template>
            </ux-table-column> -->
            <ux-table-column field="huoweibh" title="货位号">
              <template v-slot:edit="scope">
                <el-select
                  v-model="scope.row.huoweibh"
                  @change="getHuowei(scope, $event)"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in huoweilist"
                    :key="index"
                    :label="item.huoweibh"
                    :value="item.huoweibh"
                  ></el-option>
                </el-select>
              </template>
            </ux-table-column>
            <!-- <ux-table-column
              field="tuopanh"
              title="托盘号"
              :edit-render="{ autofocus: '.el-input__inner' }"
            >
              <template v-slot:edit="scope">
                <el-select
                  v-model="scope.row.tuopanh"
                  @change="getTuoPan(scope, $event)"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in tuopanlist"
                    :key="index"
                    :label="item.tuopanbh"
                    :value="item.tuopanbh"
                  ></el-option>
                </el-select>
              </template>
            </ux-table-column> -->
            <ux-table-column title="操作" width="80">
              <template v-slot="{ row }">
                <template>
                  <el-button
                    type="text"
                    @click="Acceptance(row, 1)"
                    :disabled="row.caozuozt == 3"
                  >
                    验收
                  </el-button>
                </template>
              </template>
            </ux-table-column>
          </ux-grid>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  import { queryguajiaListIDS, updateguajia } from '@/api/kunei'
  import { insertchukurwd, updatechukurwd } from '@/api/exwarehouse'
  import { queryHuoWeiList, queryTuoPanListAll } from '@/api/cangkujichu'
  export default {
    name: 'Page1Deatail',
    props: {
      gobacktitle: {
        type: String,
        default: '新增',
        required: true,
      },
      datarow: {
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        show: true,
        ruleForm: {},
        tabheight: 500,
        centerDialogVisible: false,
        huoweilist: [],
        tuopanlist: [],
        ruleslist: {
          qishid: [{ required: true, message: '请输入', trigger: 'blur' }],
          zhongjied: [{ required: true, message: '请输入', trigger: 'blur' }],
          huoweih: [{ required: true, message: '请选择', trigger: 'blur' }],
          tuopanh: [{ required: true, message: '请选择', trigger: 'blur' }],
        },
      }
    },
    created() {},
    mounted() {
      this.ruleForm = Object.assign({}, this.datarow)
      this.getGuajiaList()
    },
    methods: {
      getHuowei(scope, e) {
        let item = this.huoweilist.find((item) => {
          return item.huoweibh == e
        })
        scope.row.huoweihid = item.id
      },
      getTuoPan(scope, e) {
        let item = this.tuopanlist.find((item) => {
          return item.tuopanbh == e
        })
        scope.row.tuopanhid = item.id
      },
      async Acceptance(row, type) {
        let that = this
        let form = {}
        if (type == 1) {
          that.$refs.plxTable.validate(row).then(async (data) => {
            if (data) {
              // 有错误
            } else {
              form = {
                chukudh: that.ruleForm.chukudh,
                chukudhid: that.ruleForm.id,
                guajiamid: row.id,
                guajiam:
                  row.mingcheng + '-' + row.xingbie + '-' + row.chuchanghm,
                huoweih: row.huoweibh,
                huoweihid: row.huoweiid,
                tuopanh: row.tuopanbh,
                tuopanhid: row.tuopanid,
                qishid: row.huoweibh,
                zhongjied: '000000',
                zhuangtai: 1,
                tips: row.tips,
              }
              let p1 = new Promise((resolve, reject) => {
                updateguajia(
                  Object.assign(row, {
                    caozuozt: 3,
                  })
                )
                  .then((res) => {
                    resolve(res)
                  })
                  .catch((err) => {
                    reject(err)
                  })
              })
              let p2 = new Promise((resolve, reject) => {
                insertchukurwd(form)
                  .then((res) => {
                    resolve(res)
                  })
                  .catch((err) => {
                    reject(err)
                  })
              })
              Promise.all([p1, p2])
                .then((res) => {
                  that.$message({
                    message: '创建任务成功',
                    type: 'success',
                  })
                })
                .catch((err) => {
                  console.log(err)
                  that.$message({
                    message: '创建失败',
                    type: 'error',
                  })
                })
            }
          })
        } else {
          form = {
            chukudh: this.ruleForm.chukudh,
            chukudhid: this.ruleForm.id,
            guajiamid: row.id,
            guajiam: row.mingcheng,
            zhuangtai: 1,
          }
          const msg = await insertchukurwd(form)
          this.$message({
            message: msg.msg,
            type: 'success',
          })
        }
      },
      goBack() {
        this.$parent.acceptshow = false
      },
      async getGuajiaList() {
        queryguajiaListIDS({ ids: this.datarow.guajiaids }).then((res) => {
          this.$refs.plxTable.reloadData(res.data)
        })
        const res2 = await queryHuoWeiList()
        const res3 = await queryTuoPanListAll()
        this.huoweilist = res2.data
        this.tuopanlist = res3.data
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!')
          } else {
            return false
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      selectChange(e) {},
      uploadSuccess() {
        this.importShow = false
      },
    },
  }
</script>
<style lang="scss" scoped>
  .add-container {
    position: fixed;
    left: 0;
    top: 0.75rem;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background-color: #f6f8f9;
    padding: 0 0.2rem 1rem 0.2rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .add-top {
      padding: 0.1rem 0;

      ::v-deep .el-page-header__title {
        font-size: 0.16rem !important;
      }
    }

    .add-main-tital {
      font-size: 18px;
      border-left: 10px solid rgb(121, 189, 255);
      padding-left: 10px;
      margin: 20px 0 20px;

      p {
        margin: 0;
      }
    }

    .add-main {
      flex: 1;
      font-weight: bold;
      display: flex;
      flex-direction: column;
      overflow: auto;

      .add-main-top {
        padding: 0 20px;
        background: #fff;

        ::v-deep {
          .el-descriptions__header {
            border-left: 10px solid #79bdff;
            margin: 20px 0;
          }

          .el-descriptions__title {
            padding-left: 10px;
            font-size: 18px;
          }

          .el-descriptions-item__label {
            font-weight: bold;
            color: #333;
          }
        }
      }

      .add-main-list2 {
        flex: 1;
        background: #fff;
        padding: 0 20px;
        box-sizing: border-box;

        .list-button-group {
          display: flex;
          flex-direction: row;
          padding-bottom: 20px;

          .button-item {
            padding: 0px 10px;
          }

          .button-item:first-child,
          &:last-child {
            padding: 0px 10px 0 0;
          }
        }
      }

      .el-button {
        font-size: 0.14rem !important;
      }

      ::v-deep {
        .elx-table--header-wrapper {
          background-color: rgb(233, 243, 255);
        }

        .elx-table {
          font-size: 0.14rem !important;
        }

        .elx-body--column {
          height: 0.48rem !important;
          line-height: 0.24rem;
        }

        .elx-header--column {
          background-image: none !important;
          height: 0.48rem !important;
        }
      }

      .submit-button {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>
